
import * as constant from '../../constant'
import { deleteData, saveData, getData } from '../../utils/locationStorage'

const initUser = () => {
    //本地获取用户信息
    const currentUser = getData('currentUser')
    if (currentUser) {
        //同步到仓库state : 本地磁盘数据 => 内存数据
        return currentUser
    }
    return null
}
const initState = {
    email: '',
    username: '',
    password: '',
    errors: null,
    currentUser: initUser(),
    token: null,
    redirect: ''
}

const userReduer = (state = initState, action) => {

    switch (action.type) {
        case constant.USER_UNLOAD:
            return { ...initState }
        case constant.USER_REGIST_FIELD:  //同步
            const key = action.key
            const value = action.value
            return { ...state, [key]: value }
        case constant.USER_REGIST_RESULT:  //注册
            const result = action.result
            return { ...state, errors: result } // errors:{message:'',errors:xxx}
        case constant.USER_UPDATA_RESULT:  //更新
            if (action.result.status === 1) {
                //更新后用户信息 同步本地仓库 store
                // state.token = action.result.data.token
                console.log("USER_UPDATA_RESULT", result.currentUser);
                state.currentUser = action.result.currentUser

                //更新后用户信息同步本地 loaclstorage
                saveData('currentUser', action.result.currentUser)
                saveData('token', action.result.data.token)
            } else {
                return { ...state, ...action.result.data }
            }

            return { ...state, errors: action.result.message } // errors:{message:'',errors:xxx}
        case constant.USER_LOGIN_RESULT://登录
            // const { status, message, data } = action.result   //自己修改哒 有警告
            const { status, data } = action.result
            if (status === 1) {//登录成功
                let token = null
                let currentUser = null
                //token  :访问接口
                token = data.token
                currentUser = data
                //token  currentUser
                saveData('currentUser', currentUser)
                saveData('token', token)

                //currenrUser:  有=》登录  没有=》null没登录
                return {
                    ...state,
                    email: currentUser.email,
                    username: currentUser.username,
                    avatar: currentUser.avatar,
                    bio: currentUser.bio,
                    currentUser,
                    token,
                    errors: null,
                    redirect: "/"
                }
            } else {
                return { ...state, errors: action.result.message }
            }
        case constant.USER_SYNC_RESULT:  //登录状态同步
            const currentUser = action.result
            console.log("reducer", currentUser);
            // return { ...state, currentUser, email: currentUser.email, username: currentUser.username }
            return { ...state, currentUser, ...currentUser }
        case constant.USER_SETTING_LOGOUT:  //修改设置-退出-跳转登录
            //清除 currentUser & token
            // 内存信息 清除
            state = initState
            deleteData('currentUser')
            deleteData('token')
            return { ...state, redirect: '/login' }
        default:
            break;
    }

    return state
}

export default userReduer